package bak

import (
	"fmt"
	"gitee.com/zjzhang/weixin"
	"github.com/graarh/golang-socketio"
	"github.com/graarh/golang-socketio/transport"
	"gitee.com/zjzhang/log"
	"time"
)

const (
	URL = "ws://10.21.57.86:9093/socket.io/?X-OCS=asiainfo&EIO=3&transport=websocket"
	ACCESS_TYPE=1
)


type ocsConncet struct {
	To 					string				`json:"to"`
	From 				string				`json:"from"`
	MsgType 			string				`json:"msgType"`
	Content 			string				`json:"content"`
	AccessType 			int					`json:"accessType"`
}


type ocsMessage struct {
	To 					string				`json:"to"`
	From 				string				`json:"from"`
	MsgType 			string				`json:"msgType"`
	Content 			string				`json:"content"`
	AccessType 			int					`json:"accessType"`
}
type ocsMode struct {
	AccessSource 		string				`json:"accessSource"`
	AccessTypeDetail 	string				`json:"accessTypeDetail"`
	Client 				string				`json:"client"`
	IsForce 			string				`json:"isForce"`
	Telephone 			string				`json:"telephone"`
	ToMode 				string				`json:"toMode"`
	To 					string				`json:"to"`
}
type ocsModeResponse struct{
	CurrentModeType 	int				`json:"currentModeType"`
	MsgContent			string			`json:"msgContent"`
	UserId				string			`json:"userId"`

}

type user struct {
	sio  		*gosocketio.Client
	lastTime 	time.Time
	from 		string
	to			string

}

var (
	active = make(map[string]*user)
)

func (c *user) Close() {
	c.sio.Close()
}
func (c *user) SendMsg(msg *weixin.Message)(err error) {
	err = nil
	switch msg.MsgType {
	case weixin.MsgTypeText:
		break
	case weixin.MsgTypeImage:
		break
	case weixin.MsgTypeVoice:
		break
	case weixin.MsgTypeVideo:
		break
	case weixin.MsgTypeShortVideo:
		break
	case weixin.MsgTypeLocation:
		break
	case weixin.MsgTypeLink:
		break
	default:
		log.Errorf("unexpected receive MsgType: %s", msg.MsgType)
	}

	c.lastTime=time.Now()
	return
}
func NewUser(msg *weixin.Message) (u *user, err error) {
	u = &user{from: msg.FromUserName,to:msg.ToUserName}
	u.sio, err = gosocketio.Dial(URL, transport.GetDefaultWebsocketTransport())
	if err != nil {
		log.Fatal(err)
	}

	if err = u.sio.On("ocs_message", func(h *gosocketio.Channel, args ocsMessage) {
		relayMsg := &weixin.CustText{Content: string(args.Content)}
		log.Debugf("回复客服消息  %+v", weixin.SendCustomMsg(u.from, relayMsg))
	});err!=nil{
		u.sio.Close()
		return
	}

	if err = u.sio.On("ocs_mode", func(h *gosocketio.Channel, args ocsMessage) {
		relayMsg := &weixin.CustText{Content: string(args.Content)}
		log.Debugf("回复客服消息  %+v", weixin.SendCustomMsg(u.from, relayMsg))
	});err!=nil{
		u.sio.Close()
		return
	}
	if err = u.sio.On("message", func(h *gosocketio.Channel, args ocsMessage) {
		relayMsg := &weixin.CustText{Content: string(args.Content)}
		log.Debugf("回复客服消息  %+v", weixin.SendCustomMsg(u.from, relayMsg))
	});err!=nil{
		u.sio.Close()
		return
	}

	err=u.SendMsg(msg)
	return
}


func SendCustomMsg(msg *weixin.Message) (err error) {
	err = nil
	u, ok := active[msg.FromUserName]
	if ok {
		err=u.SendMsg(msg)
	}else{
		u,err =NewUser(msg)
	}
	return
}

func run() {
	ticker := time.NewTicker(10 * time.Second) // --- A
	defer ticker.Stop()
	for {
		for  k, v := range active {
			fmt.Print(k, v)
			if time.Now().Sub(v.lastTime).Seconds() > 10 {
				v.Close()
				delete(active, k)
			}
		}
		select {
		case <-ticker.C:
			//检测超时数据
			break
		}
	}
}
